<?php

require_once 'cms/menu/om/om/BaseMenuItemI18n.php';


/**
 * Skeleton subclass for representing a row from the 'menu_item_i18n' table.
 *
 * 
 *
 * You should add additional methods to this class to meet the
 * application requirements.  This class will only be generated as
 * long as it does not already exist in the output directory.
 *
 * @package menu.om
 */	
class MenuItemI18n extends BaseMenuItemI18n {

    private $count;

    public function getCount() {
        return $this->count;
    }

    public function setCount($count) {
        $this->count = $count;
    }

    public function hydrate(ResultSet $rs, $startcol = 1)
    {
        $startcol = parent::hydrate($rs, $startcol);

        try {
            $this->count = $rs->getInt($startcol);
        } catch(Exception $e) {
            ;
        }
    }

    /**
     * Does child menuitemI18n have a sibling with the
     * specified language code ?
     *
     * @param code The language code
     */
    function hasLanguage($code) {
        $c = new Criteria();
        $c->add(MenuItemI18nPeer::MENU_ITEM_ID, $this->getMenuItemId());
        $c->add(MenuItemI18nPeer::LANGUAGE_CODE, $code);
        return MenuItemI18nPeer::doCount($c) > 0;
    }


    /**
     * Get the already used languages for this menuitemI18n.
     *
     */
    public function getUsedLanguages() {
        if(is_null($this->usedLanguages)) {
            $criteria = new Criteria();
            $criteria->clearSelectColumns()->clearOrderByColumns();
            $criteria->addSelectColumn(MenuItemI18nPeer::LANGUAGE_CODE);
            $criteria->add(MenuItemI18nPeer::MENU_ITEM_ID, $this->getMenuItemId());
            $rs = MenuItemI18nPeer::doSelectRS($criteria);
            $codes = array();
            while($rs->next())
                $codes[] = $rs->getString(1);
            $this->usedLanguages = $codes;
        }
        return $this->usedLanguages;
    }


    /**
     * Get the available languages for this menuitemI18n.
     *
     */
    public function getAvailableLanguages() {
        if(is_null($this->usedLanguages)) {
            $used = $this->getUsedLanguages();
            $criteria = new Criteria();
            $criteria->add(LanguagePeer::CODE, $used, Criteria::NOT_IN);
            $criteria->add(LanguagePeer::I18N, true);
            $criteria->addAscendingOrderByColumn(LanguagePeer::NAME);
            $this->usedLanguages = LanguagePeer::doSelect($criteria);
        }
        return $this->usedLanguages;
    }


    /**
     * Get the name of this menuitemI18n in the CMS default language.
     *
     * @param default The default CMS language code.
     */
    function getDefaultName($default) {
        if($this->getLanguageCode() == $default)
            return $this->getName();
        else {
            $c = new Criteria();
            $c->clearSelectColumns();
            $c->addSelectColumn(MenuItemI18nPeer::NAME);
            $c->add(MenuItemI18nPeer::MENU_ITEM_ID, $this->getMenuItemId());
            $c->add(MenuItemI18nPeer::LANGUAGE_CODE, $default);
            $c->setLimit(1);
            $rs = MenuItemI18nPeer::doSelectRs($c);
            if ($rs->next())
                return $rs->getString(1);
        }
    }


    /**
     * Get the not default language menuitemI18ns with the same menuitem_id
     *
     * @param default The default CMS language code.
     */
    public function getForeignItem18ns($default, $order, $direction) {

        $c = new Criteria();

        if(strtolower($direction) == "asc")
            $c->addAscendingOrderByColumn($order);
        else
            $c->addDescendingOrderByColumn($order);

        $c->addJoin(MenuItemI18nPeer::MENU_ITEM_ID,
            MenuItemPeer::MENU_ITEM_ID);

        $c->add(MenuItemI18nPeer::MENU_ITEM_ID, $this->getMenuItemId());
        $c->add(MenuItemI18nPeer::LANGUAGE_CODE, $default, Criteria::NOT_EQUAL);

        return MenuItemI18nPeer::doSelect($c);

    }


    /**
     * OVERRIDE THIS FROM THE BASE CLASS. OTHERWISE POPULATING FORM WILL NOT WORK IN ACTIONCONTROLLER
     * WHEN FOREIGN KEY OBJECT ARE USED
     */
    public function setMenuItemId($v)
    {
        if ($this->menu_item_id !== $v) {
            $this->menu_item_id = $v;
            $this->modifiedColumns[] = MenuItemI18nPeer::MENU_ITEM_ID;
        }
    }

} // MenuItemI18n
